local internet__tanshex = fk.CreateSkill({
  name = "internet__tanshex",
  tags = { Skill.Compulsory },
})

-- 游戏开始时获得"钩爪残影"标记
internet__tanshex:addEffect(fk.GameStart, {
  anim_type = "special",
  priority = 0.1,
  mute = true,
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(self.name) and 
           (player.general == "internet__sanli" or player.deputyGeneral == "internet__sanli" or 
            player:getMark("@evol__universe_will") > 0)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    room:setPlayerMark(player, "internet__tanshex_last_color", 0)
    room:addPlayerMark(player, "@internet__tanshex_hook", 1)
  end,
})


-- 使用或打出牌时的效果
internet__tanshex:addEffect(fk.CardUsing, {
  priority = 0.0001,
  global = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("internet__tanshex") then return false end
    if not ((player.general == "internet__sanli" or player.deputyGeneral == "internet__sanli") or 
           player:getMark("@evol__universe_will") > 0) then return false end
    return target == player and data.card ~= nil
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = data.card
    if not card then return end
    
    if player:getMark("@internet__tanshex_red") > 0 then
      room:setPlayerMark(player, "@internet__tanshex_red", 0)
    end
    if player:getMark("@internet__tanshex_black") > 0 then
      room:setPlayerMark(player, "@internet__tanshex_black", 0)
    end
    
    local last_color = player:getMark("internet__tanshex_last_color")
    local current_color = 0
    if card.color == Card.Red then
      current_color = 1
      room:setPlayerMark(player, "@internet__tanshex_red", 1)
    elseif card.color == Card.Black then
      current_color = 2
      room:setPlayerMark(player, "@internet__tanshex_black", 1)
    end
    room:setPlayerMark(player, "internet__tanshex_last_color", current_color)
    
    if last_color > 0 and current_color > 0 and last_color ~= current_color then
      room:notifySkillInvoked(player, "internet__tanshex")
      player:broadcastSkillInvoke("internet__tanshex")
      
      local alive_players = room:getAlivePlayers()
      if #alive_players <= 1 then return end
      
      local targets = {}
      for _, p in ipairs(alive_players) do
        if p ~= player then
          table.insert(targets, p)
        end
      end
      if #targets == 0 then return end
      
      local target = targets[math.random(1, #targets)]
      room:sendLog{type = "#TansheSwap", from = player.id, to = {target.id}}
      local start_pos = player.seat
      local target_pos = target.seat
      room:swapSeat(player, target)
      
      local total_hooks = 0
      for _, p in ipairs(room:getAlivePlayers()) do
        total_hooks = total_hooks + p:getMark("@internet__tanshex_hook")
      end
      
      if total_hooks < 8 then
        local player_at_start = room:getPlayerBySeat(start_pos)
        if player_at_start then
          room:addPlayerMark(player_at_start, "@internet__tanshex_hook", 1)
        end
      end
      
      total_hooks = 0
      for _, p in ipairs(room:getAlivePlayers()) do
        total_hooks = total_hooks + p:getMark("@internet__tanshex_hook")
      end
      
      if total_hooks > 0 then
        local draw_num = math.floor(total_hooks / 2)
        if draw_num > 0 then
          player:drawCards(draw_num, "internet__tanshex")
        end
      end
      
      if player:hasSkill("internet__yinbaox") then
        local path_players = {}
        local n = #room.players
        local clockwise_distance = (target_pos - start_pos + n) % n
        local counter_clockwise_distance = (start_pos - target_pos + n) % n
        local is_clockwise = (clockwise_distance <= counter_clockwise_distance)
        local distance = math.min(clockwise_distance, counter_clockwise_distance)
        local current_pos = start_pos
        
        for i = 1, distance - 1 do
          if is_clockwise then
            current_pos = (current_pos % n) + 1
          else
            current_pos = ((current_pos - 2) % n) + 1
          end
          local p = room:getPlayerBySeat(current_pos)
          if p and p ~= player then
            table.insert(path_players, p)
          end
        end
        
        local start_player = room:getPlayerBySeat(start_pos)
        local target_player = room:getPlayerBySeat(target_pos)
        
        if start_player and start_player ~= player and not table.contains(path_players, start_player) then
          table.insert(path_players, start_player)
        end
        if target_player and target_player ~= player and not table.contains(path_players, target_player) then
          table.insert(path_players, target_player)
        end
        
        for _, p in ipairs(path_players) do
          if p.dead then goto continue end
          room:sendLog{type = "#YinbaoEffect", from = player.id, to = {p.id}}
          if not p:isNude() then
            local discard_num = math.random(1, 2)
            local prompt = "音爆：请弃置"..discard_num.."张牌，否则将受到1点随机属性伤害"
            local cards = room:askToDiscard(p, {
              min_num = discard_num,
              max_num = discard_num,
              optional = true,
              skill_name = "internet__yinbaox",
              pattern = ".",
              prompt = prompt
            })
            
            if #cards > 0 then
              goto continue
            end
          end
          
          if room:askToSkillInvoke(player, {
            skill_name = "internet__yinbaox", 
            prompt = "#internet__yinbaox-effect::"..p.id
          }) then
            local damage_types = {fk.NormalDamage, fk.FireDamage, fk.ThunderDamage, fk.IceDamage}
            local random_type = damage_types[math.random(1, #damage_types)]
            room:sendLog{type = "#YinbaoDamage", from = player.id, to = {p.id}}
            room:damage({
              from = player,
              to = p,
              damage = 1,
              damageType = random_type,
              skillName = "internet__yinbaox",
            })
          end
          
          ::continue::
        end
      end
    end
  end,
})



-- 响应牌时的效果
internet__tanshex:addEffect(fk.CardResponding, {
  priority = 0.1,
  global = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("internet__tanshex") then return false end
    if not ((player.general == "internet__sanli" or player.deputyGeneral == "internet__sanli") or 
           player:getMark("@evol__universe_will") > 0) then return false end
    return target == player and data.card ~= nil
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = data.card
    if not card then return end
    
    if player:getMark("@internet__tanshex_red") > 0 then
      room:setPlayerMark(player, "@internet__tanshex_red", 0)
    end
    if player:getMark("@internet__tanshex_black") > 0 then
      room:setPlayerMark(player, "@internet__tanshex_black", 0)
    end
    
    local last_color = player:getMark("internet__tanshex_last_color")
    local current_color = 0
    if card.color == Card.Red then
      current_color = 1
      room:setPlayerMark(player, "@internet__tanshex_red", 1)
    elseif card.color == Card.Black then
      current_color = 2
      room:setPlayerMark(player, "@internet__tanshex_black", 1)
    end
    room:setPlayerMark(player, "internet__tanshex_last_color", current_color)
    
    if last_color > 0 and current_color > 0 and last_color ~= current_color then
      room:notifySkillInvoked(player, "internet__tanshex")
      player:broadcastSkillInvoke("internet__tanshex")
      
      local alive_players = room:getAlivePlayers()
      if #alive_players <= 1 then return end
      
      local targets = {}
      for _, p in ipairs(alive_players) do
        if p ~= player then
          table.insert(targets, p)
        end
      end
      if #targets == 0 then return end
      
      local target = targets[math.random(1, #targets)]
      room:sendLog{type = "#TansheSwap", from = player.id, to = {target.id}}
      
      local start_pos = player.seat
      local target_pos = target.seat
      room:swapSeat(player, target)
      
      local total_hooks = 0
      for _, p in ipairs(room:getAlivePlayers()) do
        total_hooks = total_hooks + p:getMark("@internet__tanshex_hook")
      end
      
      if total_hooks < 8 then
        local player_at_start = room:getPlayerBySeat(start_pos)
        if player_at_start then
          room:addPlayerMark(player_at_start, "@internet__tanshex_hook", 1)
        end
      end
      
      total_hooks = 0
      for _, p in ipairs(room:getAlivePlayers()) do
        total_hooks = total_hooks + p:getMark("@internet__tanshex_hook")
      end
      
      if total_hooks > 0 then
        local draw_num = math.floor(total_hooks / 2)
        if draw_num > 0 then
          player:drawCards(draw_num, "internet__tanshex")
        end
      end
      
      if player:hasSkill("internet__yinbaox") then
        local path_players = {}
        local n = #room.players
        local clockwise_distance = (target_pos - start_pos + n) % n
        local counter_clockwise_distance = (start_pos - target_pos + n) % n
        local is_clockwise = (clockwise_distance <= counter_clockwise_distance)
        local distance = math.min(clockwise_distance, counter_clockwise_distance)
        local current_pos = start_pos
        
        for i = 1, distance - 1 do
          if is_clockwise then
            current_pos = (current_pos % n) + 1
          else
            current_pos = ((current_pos - 2) % n) + 1
          end
          local p = room:getPlayerBySeat(current_pos)
          if p and p ~= player then
            table.insert(path_players, p)
          end
        end
        
        local start_player = room:getPlayerBySeat(start_pos)
        local target_player = room:getPlayerBySeat(target_pos)
        
        if start_player and start_player ~= player and not table.contains(path_players, start_player) then
          table.insert(path_players, start_player)
        end
        if target_player and target_player ~= player and not table.contains(path_players, target_player) then
          table.insert(path_players, target_player)
        end
        
        for _, p in ipairs(path_players) do
          if p.dead then goto continue end
          room:sendLog{type = "#YinbaoEffect", from = player.id, to = {p.id}}
          if not p:isNude() then
            local discard_num = math.random(1, 2)
            local prompt = "音爆：请弃置"..discard_num.."张牌，否则将受到1点随机属性伤害"
            local cards = room:askToDiscard(p, {
              min_num = discard_num,
              max_num = discard_num,
              optional = true,
              skill_name = "internet__yinbaox",
              pattern = ".",
              prompt = prompt
            })
            
            if #cards > 0 then
              goto continue
            end
          end
          
          if room:askToSkillInvoke(player, {
            skill_name = "internet__yinbaox", 
            prompt = "#internet__yinbaox-effect::"..p.id
          }) then
            local damage_types = {fk.NormalDamage, fk.FireDamage, fk.ThunderDamage, fk.IceDamage}
            local random_type = damage_types[math.random(1, #damage_types)]
            room:sendLog{type = "#YinbaoDamage", from = player.id, to = {p.id}}
            room:damage({
              from = player,
              to = p,
              damage = 1,
              damageType = random_type,
              skillName = "internet__yinbaox",
            })
          end
          
          ::continue::
        end
      end
    end
  end,
})

Fk:loadTranslationTable{
  ["internet__tanshex"] = "弹射",
  [":internet__tanshex"] = "锁定技，共鸣技，①你使用/打出牌时，若此牌与上一张牌颜色不同，你随机与一名角色交换位置；②游戏开始时，你获得1枚「钩爪残影」，你交换位置后，在起始位武将牌旁留下1枚「钩爪残影」（至多为8），然后你摸X张牌，X为场上钩爪残影数量的一半（向下取整）。",
  ["@internet__tanshex_hook"] = "钩爪残影",
  ["#TansheSwap"] = "%from 的【弹射】技能被触发，与 %to 交换了位置",
  ["@internet__tanshex_red"] = "红色",
  ["@internet__tanshex_black"] = "黑色",
  ["$internet__tanshex"] = "看来要掀起最后的底牌了！",
  ["#internet__yinbaox-effect"] = "音爆：是否对 %dest 发动「音爆」效果？",
  ["#internet__yinbaox-discard"] = "音爆：请弃置%s张牌，否则将受到1点随机属性伤害",
  ["#YinbaoEffect"] = "%from 的「音爆」效果触发，要求 %to 弃置牌",
  ["#YinbaoDamage"] = "%from 对 %to 造成了1点随机属性伤害",
}

return internet__tanshex